from PySide6.QtCore import Qt, QTime, Slot
from PySide6.QtWidgets import QDialog, QMessageBox

from Devices.Chiot.BsDevs.Stripe.StripeDevice import StripeDevice
from Devices.Chiot.BsDevs.ComDev.Forms.ComTimerSetWindow import ComTimerSetWindow
from Devices.Chiot.BsDevs.Stripe.Forms.TimerSetWindow_UI import Ui_FormTimerSetWindow
from Components.MessageBoxFunc import MessageBoxFunc

class TimerSetWindow(ComTimerSetWindow, QDialog):
    def __init__(self, parent = None):
        super().__init__()
        QDialog.__init__(self, parent)

        self._ui = Ui_FormTimerSetWindow()
        self._ui.setupUi(self)

        flags = self.windowFlags()
        self.setWindowFlags(flags & ~Qt.WindowContextHelpButtonHint)
    
    def GetTimerServices(self, timer):
        services = []
        switchOnoff = self._ui.comboBox_timerSwitchCtrl.currentIndex()
        if self._ui.radioButton_switchMainCtrl.isChecked():
            services.append(StripeDevice.ServiceParams("switch", "on", switchOnoff))
        if self._ui.radioButton_switch1Ctrl.isChecked():
            services.append(StripeDevice.ServiceParams("switch1", "on", switchOnoff))
        if self._ui.radioButton_switch2Ctrl.isChecked():
            services.append(StripeDevice.ServiceParams("switch2", "on", switchOnoff))
        if self._ui.radioButton_switch3Ctrl.isChecked():
            services.append(StripeDevice.ServiceParams("switch3", "on", switchOnoff))
        if self._ui.radioButton_switch4Ctrl.isChecked():
            services.append(StripeDevice.ServiceParams("switch4", "on", switchOnoff))
        if self._ui.radioButton_switch5Ctrl.isChecked():
            services.append(StripeDevice.ServiceParams("switch5", "on", switchOnoff))
        timer.services = services
    
    def SetTimerServices(self, timer):
        if None is timer:
            return

        self._ui.comboBox_timerSwitchCtrl.setCurrentIndex(timer.services[0].paraValue)
        switchOnoff = bool(timer.services[0].paraValue)
        for service in timer.services:
            if "switch" == service.serviceId:
                self._ui.radioButton_switchMainCtrl.setChecked(switchOnoff)
            elif "switch1" == service.serviceId:
                self._ui.radioButton_switch1Ctrl.setChecked(switchOnoff)
            elif "switch2" == service.serviceId:
                self._ui.radioButton_switch2Ctrl.setChecked(switchOnoff)
            elif "switch3" == service.serviceId:
                self._ui.radioButton_switch3Ctrl.setChecked(switchOnoff)
            elif "switch4" == service.serviceId:
                self._ui.radioButton_switch4Ctrl.setChecked(switchOnoff)
            elif "switch5" == service.serviceId:
                self._ui.radioButton_switch5Ctrl.setChecked(switchOnoff)
            else:
                MessageBoxFunc.ShowMessageBox("编辑定时器","未知的服务ID:%s" % (service.serviceId), self, QMessageBox.Critical)
        